<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>修改房间信息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
    <script type="text/javascript" src="/js/cookie.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-body">
    <form class="layui-form" id="update">
     <!--   <div class="layui-form-item">
            <label for="liveId" class="layui-form-label">
                入住编号
            </label>
            <div class="layui-input-inline">
                <input type="text" id="liveId" name="liveId" required=""
                       autocomplete="off" class="layui-input" readonly>
            </div>
        </div>-->
        <div class="layui-form-item">
            <label for="rdid" class="layui-form-label">
                房间
            </label>
            <div class="layui-input-inline">
                <select id="type" name="type" lay-filter="get_type" >
                    <option value="" disabled selected hidden>房间类型</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select id="rdid" name="rdid" lay-filter="get_roomNum">
                    <option value="" id="defaultRdid" disabled selected hidden></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="cashPledge" class="layui-form-label">
                押金
            </label>
            <div class="layui-input-inline">
                <input type="text" id="cashPledge" name="cashPledge" required=""
                       autocomplete="off" class="layui-input" lay-verify="required|number" >
            </div>
        </div>
        <div class="layui-form-item">
            <label for="liveNums" class="layui-form-label">
                入住人数
            </label>
            <div class="layui-input-inline">
                <input type="text" id="liveNums" name="liveNums" required="" lay-verify="required|number"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" for="orderDate">
                入住日期
            </label>
            <div class="layui-input-inline">
                <input type="text" name="orderDate" id="orderDate" required  lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" for="leaveDate">
                退房日期
            </label>
            <div class="layui-input-inline">
                <input type="text" name="leaveDate" id="leaveDate" required  lay-verify="required" autocomplete="off" placeholder="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="delFlag" class="layui-form-label">
                住房状态
            </label>
            <div class="layui-input-inline">
                <select name="delFlag" id="delFlag" class="layui-select-group">
                    <option name="status" value="0">正入住</option>
                    <option name="status" value="1">已退房</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="payment" class="layui-form-label">
                支付金额
            </label>
            <div class="layui-input-inline">
                <input type="tel" id="payment" name="payment" required="" lay-verify="number"
                       autocomplete="off" class="layui-input" >
            </div>
        </div>
        <div class="layui-form-item">
            <label for="payStatus" class="layui-form-label">
                支付状态
            </label>
            <div class="layui-input-inline">
                <select name="payStatus" id="payStatus" class="layui-select-group">
                    <option name="status" value="0">未支付</option>
                    <option name="status" value="1">已支付</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="remark" class="layui-form-label">
                备注
            </label>
            <div class="layui-input-inline">
                <input type="tel" id="remark" name="remark" required="" lay-verify="roomTel"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="text-align: center">
            <button type="button" class="layui-btn" lay-filter="edit" lay-submit="">
                添加入住
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer','laydate'], function () {
        var roomType = [];
        var room = [];
        var index;
        var $ = layui.jquery;
        var type = $("#type");
        var roomNum = $("#rdid");
        var form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#orderDate' //指定元素
        });
        laydate.render({
            elem: '#leaveDate' //指定元素
        });

        //请求房间信息
        $.ajax({
            type: 'POST',
            url: '/checkin/getRoomDetail',
            dataType: 'json',
            success: function (data) {
                //console.log(data);
                //存储房间类型和对应号码
                for (var key in data) {
                    roomType.push(key);
                    room.push(data[key]);
                }

                //动态渲染房间类型下拉框
                for (var i = 0; i < roomType.length; i++) {
                    type.append("<option value=" + i + ">" + roomType[i] + "</option>");
                    form.render('select');
                }

                //监听房间类型的值
                form.on('select(get_type)', function (data) {
                    index = data.value;
                    /*console.log("我是监听" + index);
                    console.log(room[index]);*/

                    roomNum.empty();//渲染前先清空
                    //动态渲染对应房间号下拉框
                    for(var j=0;j<room[index].length;j++) {
                        roomNum.append("<option value=" + room[index][j] + ">" + room[index][j] + "</option>");
                        //console.log(roomNum);
                        form.render('select');
                    }
                });

                //监听房间号码的值
                form.on('select(get_roomNum)', function (data) {
                    console.log(data.value);//得到选择后的对象
                });
            }
        });

        //监听提交
        form.on('submit(edit)', function (data) {
            //发异步，把数据提交给后台
            //获取到表单数据
            //data.field为表单中的数据！
            var newData = data.field;
            delete newData.type;//去掉type数据
            if(data.field.rdid=="empty"){
                layer.alert('房间号码未选择!',{icon:5});
            }
            else {
                $.ajax({
                    url: '/checkin/addSave',
                    contentType: "application/json; charset=utf-8",
                    type: 'POST',
                    dataType:"json",
                    data: JSON.stringify(newData),
                    success: function (res) {
                        if (res.status == 0) {
                            layer.closeAll('loading');
                            layer.load(2);
                            layer.alert("添加成功", {icon: 6}, function () {
                                console.log(parent);
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                                parent.location.reload();
                            });
                        } else {
                            layer.alert("添加失败", {icon: 5})
                        }
                    },
                })
            }
            return false;
        });
    });
</script>

</body>

</html>